
import Template from '@/layouts/public/Template.vue'
import router from "@/router";
import { deepCopy, isEmptyObject } from "@/utils/tool";
import { cache } from "@/utils/cache";
import config from "@/config";
import { msgError } from "@/utils/message";
import { getUserInfo } from "@/api/user/user"
import store from '@/store'
// 读取环境配置
const AppConfig = window.AppConfig;

// import MenuList from "@/config/menu";
/**
 * 过滤器路由
 * 遍历后台传来的路由字符串，转换为组件对象
 * @param routers
 * @returns
 */
const filterRouter = (routers) => {
    routers = deepCopy(routers);

    if (isEmptyObject(routers)) {
        return false;
    }
    const _import = (component) => {
        if (component || typeof (component) == 'string') {
            return () => import("@/views" + component + ".vue");
        }
        return null;

    };
    const accessedRouters = routers.filter((route) => {
        if (route.children && route.children.length) {
            route.component = Template;
            route.children = filterRouter(route.children);
        } else {

            route.component = _import(route.component);
        }
        router.addRoute("/", route);
        return true;
    });
    return accessedRouters;
};
/**
 * 匹配当前路由是否拥有权限
 * @param 当前路由 path 
 * @param 可用权限 object 
 * @returns 
 */
// const is_arrays = (path, object) => {
//     let arr = {
//         status: false,
//         permission: []
//     };
//     for (const key in object) {
//         if (Object.hasOwnProperty.call(object, key)) {
//             if (object[key].permissionId == path) {
//                 arr.status = true;
//                 for (const k in object[key]['actionEntitySet']) {
//                     if (object[key].hasOwnProperty.call(object[key]['actionEntitySet'], k)) {
//                         arr.permission.push(object[key]['actionEntitySet'][k]['action']);
//                     }
//                 }
//                 break;
//             }
//         }
//     }
//     return arr;
// }
/**
 * 生成可用路由
 * @param 所有路由列表 routerList 
 * @param 可用权限 filters 
 * @returns 
 */
// const filterList = (routerList, filters) => {
//     let routers = deepCopy(routerList);
//     let newList = [];
//     let i = 0
//     for (const key in routers) {
//         const isArrays = is_arrays(routers[key].path, filters);
//         if (isArrays.status) {
//             newList[i] = routers[key];
//             newList[i]['meta']['permission'] = isArrays.permission;
//             if (routers[key].children && routers[key].children.length) {
//                 newList[i]['children'] = filterList(routers[key].children, filters);
//             }
//             i++;
//         }
//     }
//     return newList;
// }
// 获取菜单路由
export function getRoutMenu() {
    // 本地测试
    // return new Promise(function (resolve, reject) {
    //     if (MenuList) {
    //         // 存入菜单
    //         cache(config.MenuCacheName, MenuList);
    //         // 过滤器路由
    //         filterRouter(MenuList);
    //         resolve(MenuList);
    //     } else {
    //         reject(MenuList);
    //     }

    // });
    // 服务器正式-前台罗列路由，后台返回权列表
    // let routeList = [];
    // return new Promise(function (resolve, reject) {
    //     // 登录成功-服务器请求菜单列表
    //     MenuRoutList().then((res) => {
    //         const roles = res.data.roles;
    //         // 是超级管理员
    //         if (roles.isSuper) {
    //             routeList = MenuList;
    //         } else {
    //             routeList = filterList(MenuList, roles.permissions);
    //         }
    //         // 存入菜单
    //         cache(config.MenuCacheName, routeList);
    //         // 过滤器路由
    //         filterRouter(routeList);
    //         resolve(routeList);
    //     }).catch((res) => {
    //         reject(res);
    //     });
    // });

    // 服务器正式-后台罗列路由
    let routeList = [];
    return new Promise(function (resolve, reject) {
        // 登录成功-服务器请求菜单列表
        getUserInfo().then((res) => {

            if (res.status != 200) {
                msgError("登录失败，获取菜单菜单失败");
                reject(res);
                return false;
            }
            // 存入是否为超级权限状态
            store.setRoles(res.data.roles.isSuper);
            // 获取可用权限
            routeList = res.data.roles ? res.data.roles.permissions : [];
            if (!routeList.length) {
                msgError("无可用权限，请先分配权限");
                reject(res);
                return false;
            } else {
                cache(AppConfig.BASE_API_TOKEN_NAME + '_userid', res.data.userInfo.user_id);
                // 存入菜单
                cache(config.MenuCacheName, routeList);
                // 过滤器路由
                filterRouter(routeList);
                resolve(routeList);
            }

        }).catch((res) => {
            reject(res);
        });
    });
}
// 动态添加菜单，解决刷新页面路由报错问题
export function addMenuRoute() {
    const MenuRouteList = cache(config.MenuCacheName);
    let routeList = deepCopy(MenuRouteList);
    filterRouter(routeList);
}